package com.xingkeduo.controller.cms.marketing;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.xingkeduo.model.param.ActivityCodeParam;
import com.xingkeduo.service.cms.marketing.ActivityCodeService;
import com.xingkeduo.service.cms.marketing.CouponBagTemplateService;
import com.xingkeduo.util.Constant;
import com.xingkeduo.util.tool.ResponseMessage;

/**
 * 
 * @ClassName: ManagementCouponController
 * @Description: 市场营销管理-券处理相关
 * @author lixin
 * @date Apr 18, 2019 9:11:07 PM
 *
 */
@RestController
@RequestMapping(Constant.CMS_URL_PREFIX + "/redeemCode")
public class RedeemCodeCmsController {

	@Autowired
	ActivityCodeService activityCodeService;
	@Autowired
	CouponBagTemplateService couponBagTemplateService;

	@RequestMapping("/getRedeemCodeBatchList")
	public ResponseMessage getRedeemCodeBatchList() {
		return ResponseMessage.ok("查询兑换码批次信息");
	}

	@RequestMapping("/getRedeemCodeList")
	public ResponseMessage getRedeemCodeList(ActivityCodeParam redeemCodeExample) {
		return activityCodeService.queryRedeemCodeList(redeemCodeExample);
	}

	@RequestMapping("/getRedeemCodeDetail")
	public ResponseMessage getRedeemCodeDetail(Long redeemCodeReceiveHistoryId) {
		return activityCodeService.getRedeemCodeDetail(redeemCodeReceiveHistoryId);
	}

	@RequestMapping("/getRedeemCodeUseHisory")
	public ResponseMessage getRedeemCodeUseHisory(Long redeemCodeReceiveHistoryId) {
		return activityCodeService.getRedeemCodeUseHisory(redeemCodeReceiveHistoryId);
	}

	@RequestMapping("/auditRedeemCode")
	public ResponseMessage auditRedeemCode(Long redeemCodeId, Integer status) {
		if (redeemCodeId == null) {
			return ResponseMessage.error("参数缺失", 500);
		}
		if (status == null || status == 2) {
			return ResponseMessage.error("参数缺失", 500);
		}
		return activityCodeService.auditRedeemCode(redeemCodeId, status);
	}
	
	@RequestMapping("/getThirdRedeemCodeDetail")
	public ResponseMessage getRedeemCodeDetail(String shopId,String code) {
		if(StringUtils.isBlank(code)) {
			return ResponseMessage.error("参数缺失", 500);
		}
		code = code.replaceAll(" ", "");
		int codeLength = code.length();
		if((codeLength==12 || codeLength==10) && StringUtils.isBlank(shopId)) {
			return ResponseMessage.error("参数缺失", 500);
		}
		if(codeLength==8) {//自有
			return couponBagTemplateService.queryCouponBagByCodeOrMarketingIdOrItemId(null,null,code);
		}
		if(codeLength==10) {//美团
			return couponBagTemplateService.queryCouponBagByDianpingThirdCode(shopId,code);
		}
		if(codeLength==12) {//口碑
			return couponBagTemplateService.queryCouponBagByKoubeiThirdCode(shopId,code);
		}
		return ResponseMessage.error("兑换码无法识别",500);
	}

}
